package com.jic.market.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.util.BeanUtil;
import com.jic.market.entity.PromotionBargain;
import com.jic.market.request.PromotionBargainRequest;
import com.jic.market.response.PromotionBargainResponse;
import com.jic.market.service.PromotionBargainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description: 结算优先级Controller
 * date: 2020/3/15 14:33
 * author: zxlqueryPage
 * version: 1.0
 */
@Api(tags = {"砍价活动Controller"})
@Slf4j
@RestController
@RequestMapping("/bargainy")
public class BargainPromotionController {
    @Autowired
    PromotionBargainService promotionBargainService;

    /**
     * 分页查询
     * @param form
     * @param form
     * @return
     */
    @PostMapping("/queryPage")
    @ResponseBody
    @ApiOperation(value = "分页查询")
    public RestResult<PageResult<PromotionBargainResponse>> list(@RequestBody PromotionBargainRequest form) {
        log.info("开始获取所有列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PromotionBargain PromotionBargain = BeanUtil.copy(form, PromotionBargain.class);
        PromotionBargain.setDeleteFlag(0L);
        PageResult<PromotionBargain> result = promotionBargainService.queryPage(PromotionBargain, page);
        List<PromotionBargainResponse> PromotionBargainResponseList = result.getRows().stream().map(temp -> {
            PromotionBargainResponse PromotionBargainResponse = BeanUtil.copy(temp, PromotionBargainResponse.class);
            return PromotionBargainResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<PromotionBargainResponse> pageResult = new PageResult<>();
        pageResult.setRows(PromotionBargainResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /**
     * 分页查询
     * @param form
     * @param form
     * @return
     */
    @PostMapping("/editInfo")
    @ResponseBody
    @ApiOperation(value = "分页查询")
    public RestResult saveInfo(@RequestBody PromotionBargainRequest form) {
        PromotionBargain PromotionBargain = BeanUtil.copy(form, PromotionBargain.class);
        return promotionBargainService.insertPromotion(PromotionBargain);
    }

    /**
     * 分页查询
     * @param form
     * @param form
     * @return
     */
    @PostMapping("/queryBargainInfo")
    @ResponseBody
    @ApiOperation(value = "查询DANGE")
    public RestResult queryBargainInfo(@RequestBody PromotionBargainRequest form) {
        PromotionBargain promotionBargain = promotionBargainService.queryByPrimaryKey(form.getId());
        return RestResult.success(promotionBargain);
    }


}
